goog.provide('mitel.contacts');

goog.require('mitel.request');

/**
 * We save the contacts feed here
 * @type {Object} Contacts feed
 */
mitel.contacts.contacts = {};

mitel.contacts.phone_book = [];
mitel.contacts.name_dict = [];
mitel.contacts.fetched = false;

/**
 * Save the results of a contacts API query
 * @type {Object} Client's callback function
 */
mitel.contacts.clientCallback;

mitel.contacts.empty_phonebook = function()
{
    mitel.contacts.phone_book.length = mitel.contacts.name_dict.length = 0;
}

mitel.contacts.is_phonebook_empty = function()
{
    return (mitel.contacts.phone_book.length > 0);
}

mitel.contacts.search_exact_name = function(name)
{
}

/**
 * Save the results of a contacts API query
 * @param {!Object} response API response.
 */
mitel.contacts.saveContactResults = function(response) 
{
    mitel.contacts.fetched = true;
    mitel.contacts.contacts = response;
    mitel.contacts.clientCallback(true);
};

/**
 * Handle the case where we fail to get the contacts info
 * @param {!Object} response API response.
 */
mitel.contacts.noContactResults = function(response) 
{
  mitel.contacts.fetched = true;
  mitel.contacts.clientCallback(false);
}

mitel.contacts.grab_phone_numbers = function(feed_index)
{
    var numbers=[];
    if(true == mitel.contacts.fetched)
    {
        var entry = mitel.contacts.contacts.data['feed']['entry'][feed_index];
        var phone_numbers = entry['gd$phoneNumber'];
        if (phone_numbers)
        {
            for(var i = 0; i<phone_numbers.length; i++)
            {
                numbers.push(phone_numbers[i]['$t']);
            } 
        } 
    }
    return numbers; 
}

mitel.contacts.get_phone_numbers_from_email = function(email_addr)
{
    var numbers=[];
    if(true == mitel.contacts.fetched)
    {
        for (var i = 0; i < mitel.contacts.contacts.data['feed']['entry'].length; i++) 
        {
          var entry = mitel.contacts.contacts.data['feed']['entry'][i];
          var emails = entry['gd$email'];

          if (!emails || emails.length == 0) 
          {
            continue;
          }

          for (var j = 0; j < emails.length; j++) 
          {
              if(email_addr == emails[j]['address'])
              {
                  return mitel.contacts.grab_phone_numbers(i);
              }
          }
        }
    }
    return numbers;
}

mitel.contacts.get_name_from_email = function(email_addr)
{
    var name = "Name not given";
    if(true == mitel.contacts.fetched)
    {
        for (var i = 0; i < mitel.contacts.contacts.data['feed']['entry'].length; i++) 
        {
          var entry = mitel.contacts.contacts.data['feed']['entry'][i];
          var emails = entry['gd$email'];

          if (!emails || emails.length == 0) 
          {
            continue;
          }

          for (var j = 0; j < emails.length; j++) 
          {
              if(email_addr == emails[j]['address'])
              {
                  name = entry['title']['$t'];
                  if(!name || name == undefined)
                  {
                      name = "Name not given";
                  }
                  return name;
              }
          }
        }
    }
    return name; 
}

/**
 * Fetch user's contacts from Google Contacts API, and then invoke
 * mite.contacts.saveContactResults to save the results.
 */
mitel.contacts.fetchContactsData = function(clientCallback) {
  mitel.contacts.fetched = false;
  mitel.contacts.clientCallback = clientCallback;
  url = 'http://www.google.com/m8/feeds/contacts/default/full?alt=json';
  mitel.request.makeRequest(url,
      mitel.contacts.saveContactResults,
      mitel.contacts.noContactResults);
};
